package com.talent.jiwen_teacher.ui.widgets;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.http.result.OrderListResult;
import com.talent.jiwen_teacher.util.ImageUtil;
import com.talent.jiwen_teacher.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.talent.jiwen_teacher.util.Constant.gradeMap;

public class CourseInfoNewView extends LinearLayout {
    private ImageView headIv;
    private TextView nameTv;
    private RecyclerView memberRecyclerView;
    private TextView contentTv;
    private RecyclerView imgRecyclerView;
    private ImageView txt_test;
    private TextView gradeTv;
    private TextView timeTopTv;
    private TextView typeTv;
    private TextView countTv;
    private RelativeLayout rl_bottom;

    public CourseInfoNewView(Context context) {
        super(context);
        init();
    }

    public CourseInfoNewView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CourseInfoNewView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        inflate(getContext() , R.layout.view_course_info_new , this);
        headIv = (ImageView)findViewById(R.id.img_head);
        nameTv = (TextView)findViewById(R.id.nameTv);
        memberRecyclerView = (RecyclerView)findViewById(R.id.memberRecyclerView);
        contentTv = (TextView)findViewById(R.id.contentTv);
        imgRecyclerView = (RecyclerView)findViewById(R.id.imgRecyclerView);
        txt_test=findViewById(R.id.txt_test);
        gradeTv=findViewById(R.id.gradeTv);
        timeTopTv=findViewById(R.id.timeTopTv);
        typeTv=findViewById(R.id.typeTv);
        countTv=findViewById(R.id.home_count);
        rl_bottom=findViewById(R.id.rl_bottom);

    }

    public void bindData(OrderListResult.Order data){

        if (null != data){
            if(data.getOrderType()==1){
                txt_test.setVisibility(View.VISIBLE);
            }else {
                txt_test.setVisibility(View.GONE);
            }

            if (data.getAnswerType() == 1){
                //即时开课
                typeTv.setBackgroundResource(R.drawable.bg_corner_green_1);
                typeTv.setText(getContext().getString(R.string.immediately));
//                timeTv.setVisibility(View.GONE);
               SimpleDateFormat df = new SimpleDateFormat("MM月dd日 HH点mm分");
                timeTopTv.setText(df.format(new Date(data.getAnswerTime())));
            }else{
                //预约
                typeTv.setBackgroundResource(R.drawable.bg_corner_orange_1);
                typeTv.setText(getContext().getString(R.string.order_time));
//                timeTv.setVisibility(View.VISIBLE);
                timeTopTv.setTextColor(ContextCompat.getColor(getContext() , R.color.color_FA7D31));
                @SuppressLint("SimpleDateFormat") SimpleDateFormat df = new SimpleDateFormat("MM月dd日 HH点mm分");
                timeTopTv.setText(df.format(new Date(data.getAnswerTime())));
            }

            if(data.getGradeLevel()>0){
                gradeTv.setText("("+gradeMap.get(data.getGradeLevel())+")");
            }


            ImageUtil.loadCircleImage(getContext() , data.getAskStudentHeadImage() , headIv);
            contentTv.setText(data.getQuestionIntroduction());
            nameTv.setText(data.getAskStudentName());


            if(data.getTeachingType()==1){//1v1
                rl_bottom.setVisibility(View.GONE);
            }else if(data.getTeachingType()==2){//拼班

                List<OrderListResult.Member> memberList = new ArrayList<>();
                if (!Validators.isEmpty(data.getMemberList())){
                    memberList.clear();
                    memberList.addAll(data.getMemberList());
                }
                if(memberList.size()>0){
                    rl_bottom.setVisibility(View.VISIBLE);
                    countTv.setText(""+memberList.size());
                    LinearLayoutManager memberLayoutManager = new LinearLayoutManager(getContext());
                    memberLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    memberRecyclerView.setLayoutManager(memberLayoutManager);
                    memberRecyclerView.setAdapter(new CommonAdapter<OrderListResult.Member>(getContext() , R.layout.item_course_member , memberList){
                        @Override
                        protected void convert(ViewHolder holder, OrderListResult.Member member, int position) {
                            ImageView memberIv = holder.getView(R.id.memberIv);
                            ImageUtil.loadCircleImage(getContext() , member.getStudentHeadImage() , memberIv);
                        }
                    });
                }else{
                    rl_bottom.setVisibility(View.GONE);
                }
            }

            if (!Validators.isEmpty(data.getQuestionImages())){
                imgRecyclerView.setVisibility(View.VISIBLE);
                final List<String> imgList = new ArrayList<>();
                String[] imgs = data.getQuestionImages().split(",");
                for (int i = 0 ; i < imgs.length ; i++){
                    imgList.add(imgs[i]);
                }
                LinearLayoutManager imgLayoutManager = new LinearLayoutManager(getContext());
                imgLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                imgRecyclerView.setLayoutManager(imgLayoutManager);
                imgRecyclerView.setAdapter(new CommonAdapter<String>(getContext() , R.layout.item_add_problem_img_new , imgList){
                    @Override
                    protected void convert(ViewHolder holder, String s, final int position) {
                        ImageView imgIv = holder.getView(R.id.imgIv);
                        ImageUtil.loadCorner4Image(getContext() , s , imgIv);
                        imgIv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                ImageUtil.picturePreviewWithUrl((Activity)getContext(), position  , imgList);

                            }
                        });
                    }
                });
            }else{
                imgRecyclerView.setVisibility(View.GONE);
            }

//            ImageUtil.loadCircleImage(getContext() , data.getAskStudentHeadImage() , headIv);
//
//            nameTv.setText(data.getAskStudentName());
//            contentTv.setText(data.getQuestionIntroduction());
//
//            if(data.getGradeLevel()>0){
//                gradeTv.setText(gradeMap.get(data.getGradeLevel()));
//            }
//
//            List<OrderListResult.Member> memberList = new ArrayList<>();
//            if (!Validators.isEmpty(data.getMemberList())){
//                memberList.clear();
//                memberList.addAll(data.getMemberList());
//            }
//            LinearLayoutManager memberLayoutManager = new LinearLayoutManager(getContext());
//            memberLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//            memberRecyclerView.setLayoutManager(memberLayoutManager);
//            memberRecyclerView.setAdapter(new CommonAdapter<OrderListResult.Member>(getContext() , R.layout.item_course_member , memberList){
//                @Override
//                protected void convert(ViewHolder holder, OrderListResult.Member member, int position) {
//                    ImageView memberIv = holder.getView(R.id.memberIv);
//                    ImageUtil.loadCircleImage(getContext() , member.getStudentHeadImage() , memberIv);
//                }
//            });
//            memberRecyclerView.setVisibility(GONE);
//
//            if(data.getOrderType()==1){
//                txt_test.setVisibility(VISIBLE);
//            }else{
//                txt_test.setVisibility(GONE);
//            }
//
//
//            if (!Validators.isEmpty(data.getQuestionImages())){
//                imgRecyclerView.setVisibility(VISIBLE);
//                final List<String> imgList = new ArrayList<>();
//                String[] imgs = data.getQuestionImages().split(",");
//                for (int i = 0 ; i < imgs.length ; i++){
//                    imgList.add(imgs[i]);
//                }
//                LinearLayoutManager imgLayoutManager = new LinearLayoutManager(getContext());
//                imgLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//                imgRecyclerView.setLayoutManager(imgLayoutManager);
//                imgRecyclerView.setAdapter(new CommonAdapter<String>(getContext() , R.layout.item_add_problem_img , imgList){
//                    @Override
//                    protected void convert(ViewHolder holder, String s, final int position) {
//                        ImageView imgIv = holder.getView(R.id.imgIv);
//                        ImageUtil.loadCorner4Image(getContext() , s , imgIv);
//                        imgIv.setOnClickListener(new OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                ImageUtil.picturePreviewWithUrl((Activity)getContext(), position  , imgList);
//
//                            }
//                        });
//                    }
//                });
//            }else{
//                imgRecyclerView.setVisibility(GONE);
//            }

        }
//        else{
//            ImageUtil.loadCircleImage(getContext() , "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1532596055897&di=78ebdaa98d06f5c99a2eef7803fa9f9b&imgtype=0&src=http%3A%2F%2Fimg18.3lian.com%2Fd%2Ffile%2F201706%2F09%2Feb84aab0b8ad42cd4b6cc9bbf70ec5a7.jpg" , headIv);
//            nameTv.setText("黄雨菲");
//            contentTv.setText("dasdaskhdjashdjashdjkashdkjashdkjsahkjdhasjdhaskjd");
//
//            List<String> memberList = new ArrayList<>();
//            for (int i = 0 ; i < 5 ; i ++){
//                memberList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1532596055897&di=78ebdaa98d06f5c99a2eef7803fa9f9b&imgtype=0&src=http%3A%2F%2Fimg18.3lian.com%2Fd%2Ffile%2F201706%2F09%2Feb84aab0b8ad42cd4b6cc9bbf70ec5a7.jpg");
//            }
//            LinearLayoutManager memberLayoutManager = new LinearLayoutManager(getContext());
//            memberLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//            memberRecyclerView.setLayoutManager(memberLayoutManager);
//            memberRecyclerView.setAdapter(new CommonAdapter<String>(getContext() , R.layout.item_course_member , memberList){
//                @Override
//                protected void convert(ViewHolder holder, String s, int position) {
//                    ImageView memberIv = holder.getView(R.id.memberIv);
//                    ImageUtil.loadCircleImage(getContext() , s , memberIv);
//                }
//            });
//
//
//            final List<String> imgList = new ArrayList<>();
//            for (int i = 0 ; i < 3 ; i ++){
//                imgList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1532596055897&di=78ebdaa98d06f5c99a2eef7803fa9f9b&imgtype=0&src=http%3A%2F%2Fimg18.3lian.com%2Fd%2Ffile%2F201706%2F09%2Feb84aab0b8ad42cd4b6cc9bbf70ec5a7.jpg");
//            }
//            LinearLayoutManager imgLayoutManager = new LinearLayoutManager(getContext());
//            imgLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//            imgRecyclerView.setLayoutManager(imgLayoutManager);
//            imgRecyclerView.setAdapter(new CommonAdapter<String>(getContext() , R.layout.item_add_problem_img , imgList){
//                @Override
//                protected void convert(ViewHolder holder, String s, final int position) {
//                    ImageView imgIv = holder.getView(R.id.imgIv);
//                    ImageUtil.loadCorner4Image(getContext() , s , imgIv);
//                    imgIv.setOnClickListener(new OnClickListener() {
//                        @Override
//                        public void onClick(View view) {
//                            ImageUtil.picturePreviewWithUrl((Activity)getContext(), position  , imgList);
//
//                        }
//                    });
//                }
//            });
//        }
    }
}
